<?php

/**
 * @author Watanabex
 * @copyright 2011
 * Videos Handler Class
 */

require('dao/videosDAO.php');

class videosHandler{
    public static function getTopVideo(){
        return videosDAO::getTopVideo();
    }

	public static function getLast20(){
		$videos = videosDAO::getLast20();
		foreach($videos as $v){
			$thumbnail = self::getThumbs($v->getVideoNumber());

			$v->setThumbnail($thumbnail);
		}

		return $videos;
	}

	public static function getMainVideo($id){
		return videosDAO::getMainVideo($id);
	}

	private static function getThumbs($sVideoNumber){
		$sThumb = "http://img.youtube.com/vi/".$sVideoNumber."/default.jpg";

		return $sThumb;
	}

	/*public static function addVideos($sLink, $sTipo, $sVideoNumber, $sTitulo){
		return videosDAO::addVideo($sLink, $sTipo, $sVideoNumber, $sTitulo);
	}
	
	public static function getTop3Videos(){
		$videos = videosDAO::getTop3Videos();
		foreach($videos as $v){
			$thumbnail = self::getThumbs($v->getLink(), $v->getVideoNumber(), $v->getTipo());
			
			$v->setThumbnail($thumbnail);
			
			if(strstr($v->getLink(), "youtu")){
				$sEmbed = self::getYoutubeEmbed($v->getVideoNumber());
			}else{
				 $sEmbed = self::getVimeoEmbed($v->getVideoNumber());
			}
			$v->setEmbedLink($sEmbed);
		}

		return $videos;
	}



	private static function getYoutubeEmbed($sVideoNumber){
		$sEmbed = "<iframe title='YouTube video player' width='357' height='194'"
				." src='http://www.youtube.com/embed/"
				.$sVideoNumber."' frameborder='0' allowfullscreen></iframe>";
		return $sEmbed;
	}

	private static function getVimeoEmbed($sVideoNumber){
		$sEmbed = "<iframe src=\"http://player.vimeo.com/video/".$sVideoNumber."?title=0&amp;byline=0&amp;portrait=0\" width=\"357\" height=\"194\" frameborder=\"0\"></iframe>";

		return $sEmbed;
	}

	private static function getVimeoThumbnail($sVideoNumber){
		$url = "http://vimeo.com/api/v2/video/".$sVideoNumber.".xml";
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_TIMEOUT, 30);
		$xml = curl_exec($curl);
		curl_close($curl);

		$parse = new SimpleXMLElement($xml);
		$sThumb = (string)$parse->video->thumbnail_small;

		return $sThumb;
	}

	private static function getBigYoutubeEmbed($sVideoNumber){
		$sEmbed = "<iframe title='YouTube video player' width='600' height='335'"
				." src='http://www.youtube.com/embed/"
				.$sVideoNumber."' frameborder='0' allowfullscreen></iframe>";
		return $sEmbed;
	}

	private static function getBigVimeoEmbed($sVideoNumber){
		$sEmbed = "<iframe src=\"http://player.vimeo.com/video/".$sVideoNumber."?title=0&amp;byline=0&amp;portrait=0\" width=\"600\" height=\"335\" frameborder=\"0\"></iframe>";

		return $sEmbed;
	}


	public static function getMainVideo($videoID){
		$mainVideo = videosDAO::getMainVideo($videoID);

		if(strstr($mainVideo[0]->getLink(), "youtu")){
			$sEmbed = self::getBigYoutubeEmbed($mainVideo[0]->getVideoNumber());
		}else{
			 $sEmbed = self::getBigVimeoEmbed($mainVideo[0]->getVideoNumber());
		}
		$mainVideo[0]->setEmbedLink($sEmbed);
		
		return $mainVideo; 
	}	
	
	public static function getLast20(){
		$videos = videosDAO::getLast20();
		foreach($videos as $v){
			$thumbnail = self::getThumbs($v->getLink(), $v->getVideoNumber(), $v->getTipo());
			
			$v->setThumbnail($thumbnail);
		}
		
		return $videos;
	}			

	public static function getTop3SexyNotas(){
		$sexyNotas = videosDAO::getTop3SexyNotas();
		foreach($sexyNotas as $v){
			$thumbnail_of_youtube = self::getThumbs($v->getLink(), $v->getTipo());	
			//$arr1 = explode("_@_", $thumbnail_of_youtube);	
			//$v->setThumbnail($arr1[1]);
			$v->setEmbedLink($thumbnail_of_youtube);
		}
		
		return $sexyNotas;
	}	
	public static function getTop3Youtube(){
		$youTube =  videosDAO::getTop3Youtube();
		foreach($youTube as $v){
			$thumbnail_of_youtube = self::getThumbs($v->getLink(), $v->getTipo());
			$arr1 = explode("_@_", $thumbnail_of_youtube);	
			$v->setThumbnail($arr1[1]);
			$v->setEmbedLink($arr1[0]);			
		} 		
		
		return $youTube;
	}*/	
/*private static function getYoutubeThumbnail($sVideoNumber){
		$sThumb = "http://i.ytimg.com/vi/".$sVideoNumber."/default.jpg";	
		
		return $sThumb;
	}*/
	
}

